<?php
/**
 * Created by PhpStorm.
 * User: hongyeah
 * Date: 2015/10/22
 * Time: 23:13
 */

namespace Admin\Model;


use Common\Common\MyPage;
use Think\Model;

class DoctorsModel extends Model
{
    public function addDots($data,$schedule){
        $exist =$this->where(array('dot_number'=>$data['dot_number']))->find();

        if(!empty($exist)){
            return 0;
        }

        $result =  $this->data($data)->add();

        if(!$result){
            return 1;
        }else{
            $id = $this->where(array('dot_number'=>$data['dot_number']))->find();
            $schedule['dot_id'] = $id['dot_id'];
            $s = M('schedule');
            $s->data($schedule)->add();
            return 2;
        }
    }

    public function delDotsById($ids){
        $where['dot_id'] = array('in',$ids);
        $result = $this->where($where)->delete();
        if(!$result) return false;
        M('schedule')->where($where)->delete();
        return true;

    }

    public function edtDots($data,$schedule){
        $exist = $this->where(array('dot_number' => $data['dot_number']))->find();
        if(empty($exist)){
            return false;
        }

        $this->where(array('dot_number' => $data['dot_number']))->save($data);
        $id = $this->where(array('dot_number'=>$data['dot_number']))->find();
        $schedule['dot_id'] = $id['dot_id'];
        $s = M('schedule');
        $result = $s->where(array('dot_id' => $schedule['dot_id']))->save($schedule);
        if($result)
        return true;
        else return false;
    }

    public function getDotsByID($id){
        $data = $this->where(array('dot_id'=>$id))->find();
        $schedule = M('schedule')->where(array('dot_id'=>$id))->find();

        $data['Mon'] = $schedule['mon'];
        $data['Tue'] = $schedule['tue'];
        $data['Wed'] = $schedule['wed'];
        $data['Thu'] = $schedule['thu'];
        $data['Fri'] = $schedule['fri'];
        $data['Sat'] = $schedule['sat'];
        $data['Sun'] = $schedule['sun'];
        return $data;
    }

    public function getDotsList($search='',$page_size=15){
        $data = new \stdClass();
        if($search==''){
            $where = array(
                'dot_number' => array('LIKE','%'.$search.'%'),
            );
        }else{
            $where['dot_id'] = array('gt',1);
            $where['_string'] = ' (dot_number like '.'\'%'.$search.'%\''.')  OR ( dot_name like '.'\'%'.$search.'%\''.') ';
        }

        $count = $this->where($where)->count();

        $Page = new MyPage($count,$page_size);

        $data->show = $Page->showData();

        $dds = $this->where($where)->field('dot_id,dot_number,dot_name,dot_major')->limit($Page->firstRow.','.$Page->listRows)->select();

        $data->data = $dds;

        return $data;
    }

}